package com.example.musicbox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.musicbox.config.MyException;
import com.example.musicbox.entity.MUListen;
import com.example.musicbox.entity.vo.UserListenLife;
import com.example.musicbox.mapper.MUListenMapper;
import com.example.musicbox.service.MUListenService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.musicbox.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 帅哥是我
 * @since 2022-08-02
 */
@Service
public class MUListenServiceImpl extends ServiceImpl<MUListenMapper, MUListen> implements MUListenService {

    @Resource
    private MUListenMapper muListenMapper;

    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 用户听音乐记录
     * @param mId
     * @param uId
     */
    @Override
    public void UserListenCount(Integer mId, Integer uId) {
        try {
            QueryWrapper<MUListen> wrapper = new QueryWrapper<>();
            wrapper.eq("mId",mId).eq("uId",uId);
            MUListen muListen = muListenMapper.selectOne(wrapper);
            //当第一次听这首歌曲
            if(muListen==null){
                MUListen listen = new MUListen();
                listen.setMid(mId);
                listen.setUid(uId);
                listen.setListencount(1);
                muListenMapper.insert(listen);
                //当用户已经听过这首歌曲
            }else {
                Integer listencount = muListen.getListencount();
                muListen.setListencount(listencount+1);
                muListenMapper.updateById(muListen);
            }
        }catch (Exception e){
            throw new MyException("用户听音乐次数记录失败");
        }
    }


    /**
     * 记录用户听歌曲次数查询
     * @param mId
     * @param token
     * @return
     */
    @Override
    public Result sumListenCount(Integer mId, String token) {
        try {
            Integer uId = (Integer) redisTemplate.opsForValue().get(token);
            QueryWrapper<MUListen> wrapper = new QueryWrapper<>();
            wrapper.eq("mId",mId).eq("uId",uId);
            MUListen muListen = muListenMapper.selectOne(wrapper);
            return Result.ok("听歌次数查询成功").put("muListen",muListen);
        }catch (Exception e){
            throw new MyException("用户听歌曲次数查询失败");
        }
    }


    /**
     * 用户听歌生涯记录
     * @param token
     * @param p
     * @param s
     * @return
     */
    @Override
    public Result userListenLife(String token, Integer p, Integer s) {
        try {
            Integer uId = (Integer) redisTemplate.opsForValue().get(token);
            List<UserListenLife> list = muListenMapper.selectInfo(uId);
            PageHelper.startPage(p,s);
            PageInfo<UserListenLife> info = new PageInfo<>(list);
            return Result.ok("查询成功").put("info",info);
        }catch (Exception e){
            throw new MyException("用户生涯听歌记录查询失败");
        }
    }
}
